package cn.xk.product.service.impl;

import cn.xk.basic.service.IBaiduAiAuditService;
import cn.xk.basic.util.AjaxResult;
import cn.xk.pet.domain.PetOnlineAuditLog;
import cn.xk.product.domain.*;
import cn.xk.product.domain.Product;
import cn.xk.product.domain.ProductDetail;
import cn.xk.product.mapper.ProductDetailMapper;
import cn.xk.product.mapper.ProductMapper;
import cn.xk.product.mapper.ProductOnlineAuditLogMapper;
import cn.xk.product.service.IProductService;
import cn.xk.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    @Transactional
    @Override

    public void save(Product product) {
        //添加产品信息
        super.save(product);
        //添加产品详情
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null) {
            //设置productDetail的productId
            productDetail.setProductId(product.getId());
            //添加到数据库
            productDetailMapper.save(productDetail);
        }
    }

    @Transactional
    @Override
    public void del(Long id) {
        //删除产品详情
        productDetailMapper.delByProductId(id);
        //删除产品信息
        super.del(id);

    }

    @Transactional
    @Override
    public void update(Product product) {
        super.update(product);
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null) {
            //设置productDetail的productId
            productDetail.setProductId(product.getId());
            productDetailMapper.updateByProductId(productDetail);
        }
    }


    @Transactional
    @Override
    public AjaxResult onsale(List<Long> ids) {
        for (Long id : ids) {
            Product product = productMapper.loadById(id);
            Boolean boo1 = baiduAiAuditService.textAudit(product.getName());
            Boolean boo2 = baiduAiAuditService.imageAudit(product.getResources());
            if (boo1 && boo2) {
                //审核通过
                //product：state=1。上架时间
                productMapper.updateOnsaleState(id, new Date());
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setProductId(id);
                log.setState(1);
                log.setNote(id + "审核成功");
                productOnlineAuditLogMapper.save(log);

            } else { //添加审核日志
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setProductId(id);
                log.setState(1);
                log.setNote(id + "审核失败");
                productOnlineAuditLogMapper.save(log);
                return new AjaxResult().setSuccess(false).setMsg("上架失败");

            }

        }
        return new AjaxResult();
    }

    @Override
    public void offsale(List<Long> ids) {
        //改状态state=0,下架时间new Date()
        productMapper.updateOffsaleState(ids, new Date());
    }
}
